﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace org.xisoft.game.utils.ui
{
    /**
     * 加载对话框
     */
    public partial class Loading : UserControl
    {
        private const int count = 8;

        private int _value = 1;
        private Color _circleColor = Color.Red;
        private float _circleSize = 0.8f;

        public void DrawCircle(int j, Graphics g)
        {
            const float angle = 360.0F / count;
 

            g.TranslateTransform(Width / 2.0F, Height / 2.0F);
            g.RotateTransform(angle * _value);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.AntiAlias;
  
            for (int i = 1; i <= count; i++)
            {
                int alpha =  240/count * ((i + j - 1)%count);
                Color drawColor = Color.FromArgb(alpha, _circleColor);
                using (SolidBrush brush = new SolidBrush(drawColor))
                {
                    float sizeRate = 3.5F / _circleSize;
                    float size = Width / (5 * sizeRate);

                    float diff = (Width / 5F) - size;

                    float x = (Width / 80.0F) + diff;
                    float y = (Height / 80.0F) + diff;
                    g.FillEllipse(brush, x, y, size, size);
                    g.RotateTransform(angle);
                }
            }

        }



        int index;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (timer1.Enabled)
            DrawCircle(index, e.Graphics);
        }


        public Loading()
        {
            InitializeComponent();
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            label1.Top = (Height - label1.Height) / 2;
            label1.Left = (Width - label1.Width) / 2;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            index = (index + 1) % count;
            Invalidate();
        }


        
        public void ShowLoading()
        {
            timer1.Enabled = Visible = true;
            this.BackColor = System.Drawing.SystemColors.Control;

        }
        public void HideLoading()
        {
            Visible=timer1.Enabled = false;
            this.BackColor = System.Drawing.Color.Transparent;

        }
    }
}
